
exports.loadGPSInformation = function(origin,destination,callfunction) {
	function decodeLine(encoded) {
          var len = encoded.length;
          var index = 0;
          var array = [];
          var lat = 0;
          var lng = 0;
 
          while (index < len) {
            var b;
            var shift = 0;
            var result = 0;
            do {
              b = encoded.charCodeAt(index++) - 63;
              result |= (b & 0x1f) << shift;
              shift += 5;
            } while (b >= 0x20);
            var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
            lat += dlat;
 
            shift = 0;
            result = 0;
            do {
              b = encoded.charCodeAt(index++) - 63;
              result |= (b & 0x1f) << shift;
              shift += 5;
            } while (b >= 0x20);
            var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
            lng += dlng;
 
            array.push([lat * 1e-5, lng * 1e-5]);
          }
 
          return array;
    }
        
        
	data = [];
    var url="http://maps.googleapis.com/maps/api/directions/json?origin="+origin+"&destination="+destination+"&sensor=true";
    xhr = Titanium.Network.createHTTPClient();
    xhr.open('GET',url);
            Ti.API.info('>>> go get data for Rgeocode! ...URL: '+url);
    xhr.onload = function(){
 
        // Now parse the XML 
        var json = this.responseText;
  		//Ti.API.info(json);
  		var routes = JSON.parse(json).routes;
        var legs=routes[0].legs;
        var steps= legs[0].steps;
        var points=[];
       //Ti.API.info(steps);
       
       	 for(var i = 0; i < steps.length; i++) {
       	 	//var points=steps[i].polyline[0].points;
       	 	var polyline=steps[i].polyline;
       	 	//var points=polyline[0].points
       	 	//Ti.API.info(polyline.points);
       	 	var polylineString=polyline.points
       	 	var decodedPolyline = decodeLine(polylineString);
       	 	//Ti.API.info(decodedPolyline);
       	 	for (var j = 0; j < decodedPolyline.length; j++) {
                if (decodedPolyline[j] != null) {
                    points.push({
                        latitude : decodedPolyline[j][0],
                        longitude : decodedPolyline[j][1]
                    });
                }
            }           
       	 }
       	// Ti.API.info(points);
        callfunction({
					datajson :points,
		});
		
			
    };
    xhr.send();
};
